package com.ruoyi.hxf.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hxf.domain.vo.AdviceVo;
import com.ruoyi.hxf.domain.bo.AdviceBo;
import com.ruoyi.hxf.service.IAdviceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 反馈(校长邮箱)
 *
 * @author hxf
 * @date 2022-12-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/advice/info")
public class AdviceController extends BaseController {

    private final IAdviceService iAdviceService;
    private final ISysUserService iSysUserService;

    /**
     * 查询反馈(校长邮箱)列表
     */
    @SaCheckPermission("advice:info:list")
    @GetMapping("/list")
    public TableDataInfo<AdviceVo> list(AdviceBo bo, PageQuery pageQuery) {
        TableDataInfo<AdviceVo> adviceVoTableDataInfo = iAdviceService.queryPageList(bo, pageQuery);
        for (AdviceVo row : adviceVoTableDataInfo.getRows()) {
            row.setFeedbackName(iSysUserService.selectUserNameById(row.getFeedbackId()));
            if (row.getReplyId() != null) {
                row.setReplyName(iSysUserService.selectUserNameById(row.getReplyId()));
            }
        }
        return adviceVoTableDataInfo;
    }


    /**
     * 获取反馈(校长邮箱)详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("advice:info:query")
    @GetMapping("/{id}")
    public R<AdviceVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long id) {
        return R.ok(iAdviceService.queryById(id));
    }


    /**
     * 修改反馈(校长邮箱)
     */
    @SaCheckPermission("advice:info:edit")
    @Log(title = "反馈(校长邮箱)", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdviceBo bo) {
        bo.setReplyStatus("1");
        bo.setReplyId(LoginHelper.getUserId());
        bo.setReplyTime(DateUtils.getNowDate());
        return toAjax(iAdviceService.updateByBo(bo));
    }

    /**
     * 删除反馈(校长邮箱)
     *
     * @param ids 主键串
     */
    @SaCheckPermission("advice:info:remove")
    @Log(title = "反馈(校长邮箱)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAdviceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 获取所有的公开回复的建议
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectAllAdvice")
    public R<List<AdviceVo>> SelectAllAdvice() {
        return R.ok(iAdviceService.SelectAllAdvice());
    }

    /**
     * 根据id获取建议详细信息
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectAdviceById/{id}")
    public R<AdviceVo> SelectAdviceById(@PathVariable("id") Long id) {
        return R.ok(iAdviceService.SelectAdviceById(id));
    }

    /**
     * 获取我的建议
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectMyAdvice")
    public R<List<AdviceVo>> SelectMyAdvice() {
        Long userId = LoginHelper.getUserId();
        return R.ok(iAdviceService.SelectMyAdvice(userId));
    }

    /**
     * 修改建议(校长邮箱)
     * (手机端)
     */
    @Log(title = "修改建议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/UpdateMyAdvice")
    public R<Void> UpdateMyAdvice(@RequestBody AdviceBo bo) {
        AdviceVo adviceVo = iAdviceService.SelectAdviceById(bo.getId());
        adviceVo.setFeedbackTitle(bo.getFeedbackTitle());
        adviceVo.setFeedbackContent(bo.getFeedbackContent());
        adviceVo.setFeedbackPhoto(bo.getFeedbackPhoto());
        adviceVo.setFeedbackTime(DateUtils.getNowDate());
        iAdviceService.updateAdvice(adviceVo);
        return R.ok();
    }

    /**
     * 撤回建议(校长邮箱)
     * （手机端）
     */
    @Log(title = "撤回建议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/RecallMyAdvice/{id}")
    public R<Void> RecallMyAdvice(@PathVariable("id") Long id) {
        AdviceVo adviceVo = iAdviceService.SelectAdviceById(id);
        iAdviceService.updateAdviceById(adviceVo.getId());
        return R.ok();
    }

    /**
     * 删除建议(校长邮箱)
     * （手机端）
     */
    @Log(title = "删除建议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/DeleteMyAdvice/{id}")
    public R<Void> DeleteMyAdvice(@PathVariable("id") Long id) {
        AdviceVo adviceVo = iAdviceService.SelectAdviceById(id);
        iAdviceService.deleteAdviceById(adviceVo.getId());
        return R.ok();
    }

    /**
     * 新增建议(校长邮箱)
     * (手机端)
     */
    @Log(title = "新增建议", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add( @RequestBody AdviceBo bo) {
        Long userId = LoginHelper.getUserId();
        bo.setFeedbackId(userId);
        bo.setFeedbackTime(DateUtils.getNowDate());
        bo.setReplyStatus("0");
        bo.setIsPublic("1");
        System.out.println(bo);
        return toAjax(iAdviceService.insertByBo(bo));
    }
}
